<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../css/table.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.8.3.min.js"></script>
<script>
var pids=[];
var index =0;

	$(function(){
		loadType();
	});
	
	function loadType(){
			$.post('../getByPIDRight.action',{'right.parentid':''},function(data){
					$('#xz').html('');
					pids=data;
					for(var i=0;i<data.length;i++){
						$('#all').append('<div class="qx_parent" id="p'+data[i].id+'"><div class="parent_title" id="'+data[i].id+'">'+data[i].name+'</div></div>');
					}
					loadChild();
			},'json');
	}
	
	function loadChild(){
		if(index<pids.length){
			$.post('../getByPIDRight.action',{'right.parentid':pids[index].id},function(result){
					for(var i=0;i<result.length;i++){
						$('#p'+pids[index].id).append('<div class="qx_child" id="'+result[i].id+'">'+result[i].name+'</div>');
					}
					index++;
			},'json');
			setTimeout('loadChild();',100);
		}else{
			regCall();	
		}
		if(index==(pids.length-1)){
			setTimeout('loadUserAward();',400);
		}
	}
	
	function regCall(){
		$('#all .qx_child').click(function(){
			var tt = $('#selqx').val()+'';
			if(tt.indexOf(this.id)==-1){
				if(tt.length<1){
					tt+=this.id;	
				}else{
					tt+=','+this.id;
				}
				addR(this.id);
				$('#selqx').val(tt);
			}else{
				removeRight(this.id);
			}
			
		});
		
	}
	
	
	function loadUserAward(){
		$.post('../getByUserAward.action',{'uid':$('#uid').val()},function(data){
			for(var i=0;i<data.length;i++){
				var temp = $('#selqx').val();
				if(temp==''){
					$('#selqx').val(data[i].rid);
				}else{
					$('#selqx').val(temp+','+data[i].rid);
				}
						var title = $('#'+data[i].rid).html();
						$('#yiyou').append('<div class="qx_child sel_qx_child" id="cp'+data[i].rid+'">'+title+'</div>');
						$('#'+data[i].rid).addClass('sel_qx_child');
			}
		},'json');	
	}
	
	function addR(rid){
				$.post('addAward.action',{'uid':$('#uid').val(),'rid':rid},function(data){
					if(data=='1'){
						var title = $('#'+rid).html();
						$('#yiyou').append('<div class="qx_child sel_qx_child" id="cp'+rid+'">'+title+'</div>');
						$('#'+rid).addClass('sel_qx_child');
					}else{
						alert('操作失败，请重试！');	
					}
				});
	}
	
	function removeRight(rid){
		$.post('reMoveAward.action',{'uid':$('#uid').val(),'rid':rid},function(data){
			if(data=='1'){
					var tt = $('#selqx').val()+'';
					$('#cp'+rid).remove();
					$('#'+rid).removeClass('sel_qx_child');	
					tt =tt.replace(','+rid,'');
					tt = tt.replace(rid,'');
					$('#selqx').val(tt);
			}else{
				alert('操作失败，请重试！');	
			}
		});
	}
	
</script>
</head>

<body>
<input name="uid" type="hidden" id="uid"  value="<%=request.getParameter("uid")%>"/>
<div id="fuquan">为[ <%=request.getParameter("name")%> ]赋权</div>
<div id="yiyou">当前已拥有权限： 
  <label for="selqx"></label>
  <input name="selqx" type="hidden" disabled="disabled" id="selqx" size="150" />
<br />
				<div class="qx_child sel_qx_child">已拥有权限：</div>
</div>
<div id="all">

</div>

</body>
</html>
